<template>
  <div style="width: 100%;height:1000px;padding:40px 40px; background-color: white;border-radius: 4px">
    <div style="margin-bottom: 40px;font-size: 18px;font-weight: 530;">套餐管理</div>
    <div style="margin-bottom: 20px; display: flex">
      <div style="flex: 10;text-align: left">
        <el-input placeholder="请输入套餐名查询内容" size="small"  v-model="search.userName" style="width: 20%; margin-right: 20px"><i slot="suffix" class="el-input__icon el-icon-search"></i></el-input>
        <el-button type="success" size="small" style="border-radius: 1px;width: 100px;text-align: center" @click="load">查询</el-button>
      </div>
      <div style="flex:2;text-align: right">
        <el-button type="success" size="small" style="border-radius: 1px;width: 100px;text-align: center" @click="add">新增</el-button>
      </div>
    </div>
    <el-table :data="tableData" border stripe style="width: 100%" :fit="true" :cell-style="{ textAlign: 'center' }" :header-cell-style="{ textAlign: 'center' }">
      <el-table-column fixed prop="id" label="套餐编号" ></el-table-column>
      <el-table-column label="套餐图片">
        <template v-slot="scope">
          <el-image :src="scope.row.avatar" :preview-src-list="[scope.row.avatar]" style="width: 50px; height: 50px; border-radius: 50%"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="userName" label="套餐名称" ></el-table-column>
      <el-table-column prop="content" label="套餐内容" ></el-table-column>
      <el-table-column prop="date" label="创建时间" ></el-table-column>
      <el-table-column fixed="right" label="操作" >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
          <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.id)" style="margin-left: 10px">
            <el-button size="small" type="text" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="text-align: right;margin-top: 20px">
      <el-pagination
          background
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-size="5"
          layout="total, prev, pager, next"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog title="请填写信息" :visible.sync="dialogVisible" width="40%">
      <el-form :model="form" label-position="right" label-width="100px" style="padding-right: 40px">
        <el-form-item label="套餐名称">
          <el-input size="small" v-model="form.userName" placeholder="请输入套餐名称"></el-input>
        </el-form-item>
        <el-form-item label="商品图片">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:9090/files/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess">
            <img v-if="form.avatar" :src="form.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="套餐内容">
          <el-input size="small" v-model="form.content" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-input size="small" v-model="form.timeofmessage" placeholder="请输入时间"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="save">保 存</el-button>
      </div>
    </el-dialog>

  </div>
</template>


<script>
import request from "@/utils/request";
export default {
  created() {
    this.load();
  },
  methods: {
    handleClick(row) {
      console.log(row);
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    //  当点击页码时，当前页码数赋值给data变量pageNum
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.load();
    },
    // 分页加载表格数据
    load() {
      request.post("/Combo/page?pageNum=" + this.pageNum, this.search).then(res => {
        if (res.code === '0') {
          this.tableData = res.data.list;
          this.total = res.data.total;
        } else {
          this.$notify.error(res.msg);
        }
      });
    },
    //点击新增的按钮后，打开模态框，并在关闭时清空form中的内容
    add(){
      this.dialogVisible=true;
      this.form={};
    },
    //点击编辑的按钮后，打开模态框，回显当前行的内容
    edit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.$nextTick(() => {
        this.$refs.tx.clearFiles()
      });
      this.dialogVisible = true;
    },

    //点击保存时调用save方法，调用时根据id存在与否判断调用save的是新增还是编辑
    save(){
      if (!this.form.id){ //如果没有id 走新增接口
        request.post("/Combo",this.form).then(res =>{ //post请求把form对象传到后端，后端写逻辑把form存到数据库里
          if (res.code === '0'){ //如果接口调用成功，则把模态框关闭，重新加载一下数据库的数据
            this.$notify.success("新增成功");
            this.dialogVisible = false;
            //从数据库中重新加载数据，展示出更新后的所有用户
            this.load();
          } else {
            this.$notify.error(res.msg); // 如果不成功，返回报错信息
          }
        })
      }else {//如果有id 走编辑接口
        request.post("/Combo/edit",this.form).then(res =>{ //post请求把form对象传到后端，后端写逻辑把form存到数据库里
          if (res.code === '0'){ //如果接口调用成功，则把模态框关闭，重新加载一下数据库的数据
            this.$notify.success("修改成功");
            this.dialogVisible = false;
            //从数据库中重新加载数据，展示出更新后的数据
            this.load();
          } else {
            this.$notify.error(res.msg); // 如果不成功，返回报错信息
          }
        })

      }
    },

    // 根据ID删除某条数据
    del(id) {
      request.delete("/Combo/" + id).then(res => {
        if (res.code === '0') {
          this.$notify.success('删除成功');
          this.load();
        } else {
          this.$notify.error(res.msg);
        }
      });
    },

    handleAvatarSuccess(res) {//钩子函数，用于上传图片数据到数据库中
      this.form.avatar = res.data;
    },



  },

  // 自定义变量
  data() {
    return {
      form:{},
      dialogVisible:false,
      input:'',
      pageNum:1,
      total:0,
      search:{},
      tableData: []
    }
  },
}
</script>